<template>
  <div>
    <a-row :gutter="10" class="mb8">
      <a-col :span="1.5">
        <a-button type="primary" plain icon="a-icon-edit" size="mini" :disabled="!data.deptId" @click="handleUpdate"
          v-hasPermi="['system:dept:edit']">修改</a-button>
      </a-col>
      <a-col :span="1.5">
        <a-button type="danger" plain icon="a-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:dept:remove']">删除</a-button>
      </a-col>
    </a-row>

    <a-table v-loading="loading" :border="true" :data="dataList" @selection-change="handleSelectionChange">
      <a-table-column type="selection" width="55" align="center" />
      <a-table-column type="index" label="序号" width="55" align="center" />
      <a-table-column label="名称" align="center" prop="deptName" />
      <a-table-column label="负责人" align="center" prop="leader" />
      <a-table-column label="联系电话" align="center" prop="phone" />
      <a-table-column label="邮箱" align="center" prop="email" />
      <a-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <a-button size="mini" type="text" icon="a-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:dept:remove']">删除
          </a-button>
        </template>
      </a-table-column>
    </a-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <br />

    <!-- 添加或修改对话框 -->
    <a-modal :title="title" :visible.sync="open" width="760px" append-to-body>
      <a-form ref="form" :model="data" :rules="rules" label-width="120px">
        <a-form-item label="组织名称" prop="deptName">
          <a-input v-model="data.deptName" :disabled="true" placeholder="请输入名称" />
        </a-form-item>
        <a-form-item label="关联组织" prop="deptName">
          <a-transfer v-model="dataScopeRelateArray" :props="{key: 'deptId',label: 'deptName'}" :titles="['未关联', '已关联']" :data="unitOptions">
          </a-transfer>
        </a-form-item>
      </a-form>
      <div slot="footer" class="dialog-footer">
        <a-button type="primary" @click="submitForm">确 定</a-button>
        <a-button @click="cancel">取 消</a-button>
      </div>
    </a-modal>
  </div>
</template>

<script>
  import {
    listDept,
    getDept,
    delDept,
    addDept,
    updateDept
  } from "@/api/system/dept";

  export default {
    name: "UnitTable",
    props: {
      ancestors: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        dataList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
        // 组织树选项
        unitOptions: undefined,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          unitId: [{
            required: true,
            message: "所属组织不能为空",
            trigger: "blur"
          }],
          providerId: [{
            required: true,
            message: "客户组织不能为空",
            trigger: "blur"
          }]
        },
        data: {},
        dataScopeRelateArray: []
      };
    },
    created() {
      this.getList();
      this.getUnitList();
    },
    methods: {
      setData(data) {
        this.data = data;
        this.getList();
      },
      /** 查询组织下拉树结构 */
      getUnitList() {
        listDept({
          ancestors: this.ancestors
        }).then(response => {
          let unitOptions = this.handleTree(response.data, "deptId");
          this.unitOptions = unitOptions[0].children;
        });
      },
      /** 查询列表 */
      getList() {
        this.loading = true;
        listDept(this.queryParams).then(response => {
          this.dataList = [];
          let data = response.data;
          let dataScopeRelateArray = JSON.parse('[' + (this.data.dataScopeRelate || '') + ']');
          this.dataScopeRelateArray = dataScopeRelateArray;
          for (let idx in data) {
            let item = data[idx];
            if (dataScopeRelateArray.indexOf(item.deptId) > -1) {
              this.dataList.push(item);
            }
          }
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {};
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.deptId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate() {
        this.reset();
        this.open = true;
        this.title = "修改关联";
      },
      /** 提交按钮 */
      submitForm() {
        let that = this;
        let dataScopeRelateArray = that.dataScopeRelateArray;
        that.data.dataScopeRelate = dataScopeRelateArray.join(',');
        updateDept(that.data).then(() => {
          that.getList();
          that.msgSuccess("修改成功");
          that.open = false;
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = this.ids || [];
        if (row && row.deptId) {
          ids.push(row.deptId);
        }
        let that = this;
        this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          let arr = new Array();
          let dataScopeRelateArray = that.dataScopeRelateArray;
          for (let idx in dataScopeRelateArray) {
            let item = dataScopeRelateArray[idx];
            if (ids.indexOf(item) == -1) {
              arr.push(item);
            }
          }
          that.data.dataScopeRelate = arr.join(',');
          updateDept(that.data).then(() => {
            that.getList();
            that.msgSuccess("删除成功");
          });
        });
      }
    }
  };
</script>
